//
//  SDDetectionReportCell2.m
//  skinDetection
//
//  Created by  liangzhun on 2018/11/6.
//  Copyright © 2018年  liangzhun. All rights reserved.
//

#import "SDDetectionReportCell2.h"
#import "SDSkinAnalysisCell3.h"
#import "SDdetectionReportFooterView.h"
#import "SDDetectionReportData.h"

static NSString *const cellIden1 = @"SDSkinAnalysisCell3";
static NSString *const headerIden = @"SDdetectionReportFooterView";
@interface SDDetectionReportCell2 ()
@property (nonatomic ,strong)SDdetectionReportFooterView *headerView;
@end
@implementation SDDetectionReportCell2

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
     [self.tableView registerNib:[UINib nibWithNibName:cellIden1 bundle:nil] forCellReuseIdentifier:cellIden1];
     [self.tableView registerNib:[UINib nibWithNibName:headerIden bundle:nil] forHeaderFooterViewReuseIdentifier:headerIden];
    //添加侧滑手势
    UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(doSwipe:)];
    leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
    
    UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(doSwipe:)];
    rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
    
    for (UIGestureRecognizer *gesture in self.tableView.gestureRecognizers) {
        [self.tableView removeGestureRecognizer:gesture];
    }
    
    [self.tableView addGestureRecognizer:leftSwipe];
    [self.tableView addGestureRecognizer:rightSwipe];
    
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    
    SDSkinAnalysisCell3 *cell = [tableView dequeueReusableCellWithIdentifier:cellIden1];
    
    if (self.contentArray.count > 2) {
        
        NSString *seperate = @"     ";
        NSString *content = self.contentArray[indexPath.section];
        
        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
        style.lineSpacing = 5;
        style.paragraphSpacing = 5;
        if([content containsString:@"• "])
        {
            [style setFirstLineHeadIndent:-10];
            [style setHeadIndent:10];
        }
        
        content = indexPath.section == 1? [content stringByReplacingOccurrencesOfString:@"：" withString:seperate]:content;
        NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:content];
        [att addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, content.length)];
        
        UIColor *contentColor = indexPath.section == 2? SDMainColor:SDMainTextColor;
        [att addAttribute:NSForegroundColorAttributeName value:contentColor range:NSMakeRange(0, att.length)];
        if (indexPath.section == 1&&[content componentsSeparatedByString:seperate].count > 2) {
            
            NSString *firstTitle = [content componentsSeparatedByString:seperate].firstObject;
            
            [att addAttribute:NSForegroundColorAttributeName value:SDMainColor range:NSMakeRange(0, firstTitle.length)];
            NSString *second = [content componentsSeparatedByString:seperate][1];
            [att addAttribute:NSForegroundColorAttributeName value:SDMainColor range:NSMakeRange(second.length + seperate.length, firstTitle.length)];
            
        }else if(indexPath.section == 0)
        {
            NSString *firstTitle = [content componentsSeparatedByString:@"\r\n"].firstObject;
            [att addAttribute:NSForegroundColorAttributeName value:SDMainColor range:NSMakeRange(0, firstTitle.length)];
            
        }
        [cell.contentLab setAttributedText:att];
        
    }
    
    return cell;
    
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.contentArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [self getReasonTextHeightWithIndexPath:indexPath];
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 70;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SDMainScreenWidth, 70)];
    UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(15, 30, SDMainScreenWidth - 30, 18)];
    NSString *title = section == 0?@"诊断":section == 1?@"分析":@"建议";
    [titleLab setText:title];
    [titleLab setTextColor:SDMainTextColor];
    [titleLab setFont:Font(18)];
    [header addSubview:titleLab];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(15, 53, 36, 2)];
    [line setBackgroundColor:SDMainTextColor];
    line.layer.cornerRadius = 1.f;
    line.layer.masksToBounds = YES;
    [header addSubview:line];
    
    return header;
    
}
//- (CGFloat)getAllHeight
//{
//    CGFloat height = 0.0;
//    for (int c = 0; c < self.contentArray.count; c++) {
//        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:c];
//        height = height + [self getReasonTextHeightWithIndexPath:indexPath];
//    }
//    return height + 150 + 57;
//}
- (CGFloat)getReasonTextHeightWithIndexPath:(NSIndexPath *)indexPath
{
    
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.lineSpacing = 5;
    style.paragraphSpacingBefore = 5;
    
    NSString *seperate = @"     ";
    NSString *content = self.contentArray[indexPath.section];
    content = indexPath.section ==2? [content stringByReplacingOccurrencesOfString:@"：" withString:seperate]:content;
    NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:content];
    [att addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, content.length)];
    
    UIColor *contentColor = indexPath.section == 2? SDMainColor:SDMainTextColor;
    [att addAttribute:NSForegroundColorAttributeName value:contentColor range:NSMakeRange(0, att.length)];
    if (indexPath.section == 1&&[content componentsSeparatedByString:seperate].count > 2) {
        
        NSString *firstTitle = [content componentsSeparatedByString:seperate].firstObject;
        
        [att addAttribute:NSForegroundColorAttributeName value:SDMainColor range:NSMakeRange(0, firstTitle.length)];
        NSString *second = [content componentsSeparatedByString:seperate][1];
        [att addAttribute:NSForegroundColorAttributeName value:SDMainColor range:NSMakeRange(second.length + seperate.length, firstTitle.length)];
        
    }else if(indexPath.section == 0&&[content containsString:@"："])
    {
        NSString *firstTitle = [content componentsSeparatedByString:@"。"].firstObject;
        
        [att addAttribute:NSForegroundColorAttributeName value:SDMainColor range:NSMakeRange(0, firstTitle.length)];
        
    }
    
    return [SDMethodTool getAttTextHeightWithText:att withTextFontSize:Font(15) Width:SDMainScreenWidth - 30] + 2;
    
}
- (UIView *)headerView
{
   
    if (!_headerView) {
       _headerView = [SDMethodTool getViewWithViewName:headerIden];
       
        [_headerView.bgView.layer setCornerRadius:3];
        [_headerView.bgView.layer setMasksToBounds:YES];
        [self addSubview:_headerView];
        //@weakify(self)
        [_headerView.skinTypeBtnArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            UIButton *skinBtn = obj;
            skinBtn.tag = idx + 1;
            [skinBtn setTitleColor:SDMainTextColor forState:UIControlStateNormal];
           
            if (self.selectType == idx + 1)
            {
                self->_headerView.selectedBtn = skinBtn;
                self->_headerView.moveView.center = skinBtn.center;
                [skinBtn setTitleColor:SDWhiteColor forState:UIControlStateNormal];
                
            }
            
            [skinBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
                if (self->_headerView.selectedBtn != skinBtn) {
                    
                     [self->_headerView.selectedBtn setTitleColor:SDMainTextColor forState:UIControlStateNormal];
                    //footerView.moveView.center = skinBtn.center;
                    //判断动画的方向
                    if (skinBtn.tag - self->_headerView.selectedBtn.tag > 0) {
                         [self Animation:YES view:self.tableView];
                    }else{
                        
                         [self Animation:NO view:self.tableView];
                    }
                    self->_headerView.selectedBtn = skinBtn;
                    self.selectType = skinBtn.tag;
                    
                    [UIView animateWithDuration:0.15 animations:^{
                        self->_headerView.moveView.center = self->_headerView.selectedBtn.center;
                    } completion:^(BOOL finished) {
                        
                        [skinBtn setTitleColor:SDWhiteColor forState:UIControlStateNormal];
                        [self.tableView reloadData];
                        if (self.block)self.block(self.selectType);
                        
                    }];
                    
                }
            }];
            
            
        }];
    }
    return _headerView;
}
- (void)setShowModel:(SDDetectionReportModel *)showModel
{
    _showModel = showModel;
    self.selectType = 1;
   
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self);
        make.height.mas_equalTo(57);
    }];
    [self.tableView reloadData];
}
-(void)setSelectType:(NSInteger)selectType
{
    _selectType = selectType;
    
    SDDetectionReportData *data = self.showModel.data;
     self.contentArray = self.selectType == 1? data.resImg1List:self.selectType == 2? data.resImg2List:self.selectType == 3? data.resImg3List:self.selectType == 4? data.resImg4List:data.resImg5List;
    
}

- (void)doSwipe:(UISwipeGestureRecognizer *)swipe
{
    if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {

        if (self.selectType == 5)return;
        self.selectType = self.selectType + 1;
        [self Animation:YES view:self.tableView];
    }
    else if (swipe.direction == UISwipeGestureRecognizerDirectionRight)
    {

         if (self.selectType == 1)return;
        self.selectType = self.selectType - 1;
        
        [self Animation:NO view:self.tableView];
    }
  
    UIButton *selectBtn = (UIButton *)[self viewWithTag:self.selectType];

    [self.headerView.selectedBtn setTitleColor:SDMainTextColor forState:UIControlStateNormal];
    self.headerView.selectedBtn = selectBtn;
    [UIView animateWithDuration:0.15 animations:^{
        self->_headerView.moveView.center = self->_headerView.selectedBtn.center;
    } completion:^(BOOL finished) {
        
        [selectBtn setTitleColor:SDWhiteColor forState:UIControlStateNormal];
        [self.tableView reloadData];
        if (self.block)self.block(self.selectType);
    }];
    
}
- (void)Animation:(BOOL)direction view:(UIView *)view
{
    //动画效果
    CATransition *animation = [CATransition animation];
    animation.duration = 0.75f;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.fillMode = kCAFillModeForwards;
    animation.type = kCATransitionPush;
    if (direction) {//左滑
        animation.subtype = kCATransitionFromRight;
    }
    else
    {
        animation.subtype = kCATransitionFromLeft;
    }
    [view.layer addAnimation:animation forKey:@"animation"];

}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
